package engine.pool
{
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	/**
	 *	资源池,所有存放入资源池的对象需要实现IResoule接口 
	 * @author lw
	 * 
	 */
	public class ObjectPool
	{
		private static var _pool : Dictionary = new Dictionary( true );
		
		private var _template	: Class;
		
		private var _list		: Array; 
		
		public function ObjectPool( value : Class )
		{
			_template 	=	value;
			_list		=	[];
		}
		
		public function borrowObject():IResoule
		{
			if( _list.length > 0 )
			{
				return _list.shift();
			}
			
			return new _template();
		}
		
		public function returnObject( value : IResoule ):void
		{
			value.reset();
			
			_list.push( value );
		}
		
		public static function GetPool( value : Class ):ObjectPool
		{
			if( null == _pool[value] )
			{
				_pool[value] = new ObjectPool( value );
			}
			return _pool[value];
		}
		
		public static function returnPool( value : IResoule ):void
		{
			var cla : Class = getDefinitionByName( getQualifiedClassName( value ) ) as Class;
			
			GetPool( cla ).returnObject( value );
		}
	}
}